from flask import Flask, request, session
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from exts import db
from models import Article, User, Comment
import config

app = Flask(__name__)
app.config.from_object(config)
# 设置允许跨域请求
CORS(app, supports_credentials=True)
db.init_app(app)


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        print('get')
    else:
        print(request.data)
        data = eval(request.data.decode())
        name = data['name']
        password = data['password']
        tele = data['tele']
        # 手机号验证，如果被注册，就不能再注册
        user = User.query.filter(User.tele == tele).first()
        # 账户名验证，如果被注册，就不能再注册
        userName = User.query.filter(User.name == name).first()
        if user:
            return u'tele error'
        elif userName:
            return u'name error'
        else:
            user = User(name=name, password=password, tele=tele)
            db.session.add(user)
            db.session.commit()
            return 'post ok'


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        print('get')
    else:
        print(request.data)
        data = eval(request.data.decode())
        name = data['name']
        password = data['password']
        user = User.query.filter(User.name == name, User.password == password).first()
        if user:
            session['user_id'] = user.id
            session['user_name'] = user.name
            session.permanet = True  # 保存登录状态
        else:
            return u'账户或密码错误'
        user_id = session['user_id']
        return str(user_id)


@app.route('/publish', methods=['GET', 'POST'])
def publish():
    if request.method == 'GET':
        print('get')
    else:
        # print(request.data)
        data = eval(request.data.decode())
        author_id = data[0]
        content = data[1]
        user = User.query.filter(User.id == author_id).first()
        print(author_id, content)
        article = Article(content=content, author_id=author_id, author=user)
        db.session.add(article)
        db.session.commit()
        return 'publish ok'


@app.route('/article_list', methods=['GET', 'POST'])
def show_post():
    articles = Article.query.all()
    result = {}
    i = 0
    for article in articles:
        post = [article.author_id, article.content, article.id]
        result[i] = post
        i = i + 1
    return result


@app.route('/article_detail', methods=['GET', 'POST'])
def article_get():
    if request.method == 'GET':
        return 'only get'
    else:
        data = eval(request.data.decode())
        print(data)
        article_id = data[0]
        article = Article.query.filter(Article.id == article_id).first()
        print(article.content)
        return article.content


@app.route('/comment_post', methods=['GET', 'POST'])
def comment_post():
    if request.method == 'GET':
        return 'only post'
    else:
        data = eval(request.data.decode())
        print(data)
        user_id = data[0]
        user = User.query.filter(User.id == user_id).first()
        article_id = data[1]
        article = Article.query.filter(Article.id == article_id).first()
        comment_content = data[2]
        comment = Comment(content=comment_content, author_id=user_id, author=user, article_id=article_id,
                          article=article)
        db.session.add(comment)
        db.session.commit()
        return 'post ok'


@app.route('/get_comments', methods=['GET', 'POST'])
def get_comment():
    if request.method == 'GET':
        return 'only post'
    else:
        data = eval(request.data.decode())
        article_id = data[0]
        comment_list = Comment.query.filter(Comment.article_id == article_id).all()
        print(comment_list)
        result = {}
        i = 0
        for comment in comment_list:
            result[i] = [comment.author_id, comment.content]
            i += 1
        return result


db.create_all(app=app)

if __name__ == '__main__':
    db.create_all(app=app)
    app.run()
